//DevNETCoder
//CopyRight By DevNET Group
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using STT.Entities;
using STT.DataAccess;
 
namespace STT.Business
{
    public class BTbNgoaiNgu
    {
//-----------------------------------------------------------//
        public static DataTable SelectAll()
        {
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbNgoaiNgu_SelectAll", null);
            return all;
        }
        public static DataTable SelectAll1()
        {
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbNgoaiNgu_SelectAll1", null);
            return all;
        }
        public static DataTable SelectTop(int Top)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"Top", Top);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbNgoaiNgu_SelectTop", pr);
            return all;
        }
//-----------------------------------------------------------//
        public static void Insert(ETbNgoaiNgu OTbNgoaiNgu)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"TenNgoaiNgu", OTbNgoaiNgu.TenNgoaiNgu);
            pr[1] = new SqlParameter(@"TrangThai", OTbNgoaiNgu.TrangThai);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbNgoaiNgu_Insert", pr);
        }
//-----------------------------------------------------------//
        public static void Update(ETbNgoaiNgu OTbNgoaiNgu)
        {
            SqlParameter[] pr = new SqlParameter[3];
            pr[0] = new SqlParameter(@"MaNgoaiNgu", OTbNgoaiNgu.MaNgoaiNgu);
            pr[1] = new SqlParameter(@"TenNgoaiNgu", OTbNgoaiNgu.TenNgoaiNgu);
            pr[2] = new SqlParameter(@"TrangThai", OTbNgoaiNgu.TrangThai);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbNgoaiNgu_Update", pr);
        }
//-----------------------------------------------------------//
        public static void Delete(int MaNgoaiNgu)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaNgoaiNgu",MaNgoaiNgu);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "TbNgoaiNgu_Delete", pr);
        }
        //-----------------------------------------------------------//

        public static bool KiemTraDuLieu_Sua(ETbNgoaiNgu NgoaiNgu)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"TenNgoaiNgu", NgoaiNgu.TenNgoaiNgu);
            pr[1] = new SqlParameter(@"MaNgoaiNgu", NgoaiNgu.MaNgoaiNgu);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbNgoaiNgu_KiemTraDuLieu_Sua", pr);
            if (all.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
         
//-----------------------------------------------------------//

        public static bool KiemTraDuLieu(ETbNgoaiNgu NgoaiNgu)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"TenNgoaiNgu", NgoaiNgu.TenNgoaiNgu);
           
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbNgoaiNgu_KiemTraDuLieu", pr);
            if (all.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
//-----------------------------------------------------------//
        public static DataTable SearchNgoaiNgu(ETbNgoaiNgu NgoaiNgu)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"TenNgoaiNgu", NgoaiNgu.TenNgoaiNgu);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbNgoaiNgu_Search", pr);
            return all;
        }
//-----------------------------------------------------------//
        public static ETbNgoaiNgu SelectByID(int MaNgoaiNgu)
        {
            ETbNgoaiNgu OTbNgoaiNgu = new ETbNgoaiNgu();
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaNgoaiNgu",MaNgoaiNgu);
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure,"TbNgoaiNgu_SelectByID", pr);
            if (idr.Read())
              OTbNgoaiNgu = GetOneTbNgoaiNgu(idr);
            idr.Close();
            idr.Dispose();
            return OTbNgoaiNgu;
        }
//-----------------------------------------------------------//
        private static ETbNgoaiNgu GetOneTbNgoaiNgu(IDataReader idr)
       {
            ETbNgoaiNgu OTbNgoaiNgu = new ETbNgoaiNgu();
                if (idr["MaNgoaiNgu"] != DBNull.Value)
                    OTbNgoaiNgu.MaNgoaiNgu = (int)idr["MaNgoaiNgu"];
                if (idr["TenNgoaiNgu"] != DBNull.Value)
                    OTbNgoaiNgu.TenNgoaiNgu = (string)idr["TenNgoaiNgu"];
                if (idr["TrangThai"] != DBNull.Value)
                    OTbNgoaiNgu.TrangThai = (int)idr["TrangThai"];
            return OTbNgoaiNgu;
       }
//-----------------------------------------------------------//
        public static bool TestByID(int MaNgoaiNgu)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"MaNgoaiNgu",MaNgoaiNgu);
            pr[1] = new SqlParameter(@"TestID",SqlDbType.Bit);
            pr[1].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbNgoaiNgu_TestByID", pr);
            return Convert.ToBoolean(pr[1].Value);
        }
//-----------------------------------------------------------//
        public static List<ETbNgoaiNgu> ListAll()
        {
            List<ETbNgoaiNgu> list = new List<ETbNgoaiNgu>();
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "TbNgoaiNgu_SelectAll", null);
            while (idr.Read())
              list.Add(GetOneTbNgoaiNgu(idr));
            if (idr.IsClosed == false)
            {
               idr.Close();
               idr.Dispose();
            }
            return list;
        }

        public static List<ETbNgoaiNgu> ListTop(int Top)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"Top", Top);
            List<ETbNgoaiNgu> list = new List<ETbNgoaiNgu>();
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "TbNgoaiNgu_SelectAll", pr);
            while (idr.Read())
              list.Add(GetOneTbNgoaiNgu(idr));
            if (idr.IsClosed == false)
            {
               idr.Close();
               idr.Dispose();
            }
            return list;
        }
    }
 
}